package oldking.wang;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Sheet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main
{

    public static void main(String[] args)
    {
        try
        {
            String sheetName = args[0];
            String excelPath = args[1];
            String templatePath = args[2];
            String savePath = args[3];

            File excel = new File(excelPath);
            String template = ReadAllBytes(templatePath);

            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excel));
            HSSFWorkbook excelFile = new HSSFWorkbook(fs);
            Sheet sheet = excelFile.getSheet(sheetName);

            String code = new LogicMain().Run(sheet, template);
            WriteString2File(code, savePath);

            System.out.println("============\n\n");
            System.out.println(code);
            System.out.println("============\n\n");
            System.out.println("All Done");

        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static String ReadAllBytes(String filePath)
    {
        String content = "";
        try
        {
            content = new String(Files.readAllBytes(Paths.get(filePath)));
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return content;
    }

    private static void WriteString2File(String _s, String _filePath)
    {
        try
        {
            Files.write(Paths.get(_filePath), _s.getBytes());
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
